package com.openarena.model.dao;

import com.openarena.model.GameTeam;
import com.openarena.model.Game;
import com.openarena.model.GameTeamEnum;
import com.openarena.model.Player;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

public class GameTeamDAO extends InfArenaDAO {

    private static final Logger logger = Logger.getLogger(GameTeamDAO.class.getCanonicalName());

    public GameTeamDAO() {
    }

    public GameTeam getTeamByGameAndPlayer(Game game, Player player) {
        Session session = null;
        try {
            session = getSession();
            return (GameTeam) session.createCriteria(GameTeam.class)
                    .add(Restrictions.eq("gameId", game.getId()))
                    .add(Restrictions.eq("playerId", player.getId()))
                    .uniqueResult();
        } catch (Exception e) {
            logger.log(Level.SEVERE, "", e);
            return null;
        } finally {
            close(session);
        }
    }

    public List<GameTeam> getTeamsByGame(Game game) {
        Session session = null;
        try {
            session = getSession();
            return session.createCriteria(GameTeam.class)
                    .add(Restrictions.eq("gameId", game.getId()))
                    .list();
        } catch (Exception e) {
            logger.log(Level.SEVERE, "", e);
            return null;
        } finally {
            close(session);
        }
    }

    public List<GameTeam> getTeamByGameAndTeam(Game game, GameTeamEnum team) {
        Session session = null;
        try {
            session = getSession();
            return session.createCriteria(GameTeam.class)
                    .add(Restrictions.eq("gameId", game.getId()))
                    .add(Restrictions.eq("team", team.toString()))
                    .list();
        } catch (Exception e) {
            logger.log(Level.SEVERE, "", e);
            return null;
        } finally {
            close(session);
        }
    }
}
